﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;

namespace ROVController
{
	public partial class Form1 : Form
	{
		SerialPort serialPort = new SerialPort();

		ROV rov;

		public Form1()
		{
			InitializeComponent();
			this.rov = new ROV(serialPort);
		}

		private void btnConnectTether_Click(object sender, EventArgs e)
		{
			if (this.btnConnectTether.Text.Equals("Connect"))
			{
				//try
				{
					serialPort.BaudRate = 115200;
					serialPort.PortName = (string)cbbPortNameTether.SelectedItem;
					serialPort.Open();
					this.cbbPortNameTether.Enabled = false;
					this.btnConnectTether.Text = "Disconnect";
					this.lblConnectionTether.Text = "Connected on " + serialPort.PortName;
					timerControl.Start();
				}
				/*catch (Exception ex)
				{

				}*/
			}
			else
			{
				timerControl.Stop();
				serialPort.Close();
				this.cbbPortNameTether.Enabled = true;
				this.btnConnectTether.Text = "Connect";
				this.lblConnectionTether.Text = "Disconnected";
			}
		}

		private void cbbPortNameTether_DropDown(object sender, EventArgs e)
		{
			object previouslySelected = cbbPortNameTether.SelectedItem;
			string[] ports = SerialPort.GetPortNames();
			cbbPortNameTether.Items.Clear();
			cbbPortNameTether.Items.AddRange(ports);
			cbbPortNameTether.SelectedItem = previouslySelected;
		}

		private void cameraControl1_PositionUpdated(object sender, PositionEventArgs e)
		{
			rov.CameraTilt = e.Position;
		}

		private void timerControl_Tick(object sender, EventArgs e)
		{
			rov.ControlROV();
		}

		int torpedo1Clicks = 0;
		private void btnLaunchTorpedo1_Click(object sender, EventArgs e)
		{
			torpedo1Clicks++;
			if (torpedo1Clicks == 1)
			{
				btnLaunchTorpedo1.BackColor = Color.Red;
			}
			else if (torpedo1Clicks == 2)
			{
				btnLaunchTorpedo1.BackColor = Color.FromKnownColor(KnownColor.ControlDarkDark);
				btnLaunchTorpedo1.Enabled = false;
				rov.FireTorpedo(1);
			}
		}

		int torpedo2Clicks = 0;
		private void btnLaunchTorpedo2_Click(object sender, EventArgs e)
		{
			torpedo2Clicks++;
			if (torpedo2Clicks == 1)
			{
				btnLaunchTorpedo2.BackColor = Color.Red;
			}
			else if (torpedo2Clicks == 2)
			{
				btnLaunchTorpedo2.BackColor = Color.FromKnownColor(KnownColor.ControlDarkDark);
				btnLaunchTorpedo2.Enabled = false;
				rov.FireTorpedo(2);
			}
		}

		int torpedo3Clicks = 0;
		private void btnLaunchTorpedo3_Click(object sender, EventArgs e)
		{
			torpedo3Clicks++;
			if (torpedo3Clicks == 1)
			{
				btnLaunchTorpedo3.BackColor = Color.Red;
			}
			else if (torpedo3Clicks == 2)
			{
				btnLaunchTorpedo3.BackColor = Color.FromKnownColor(KnownColor.ControlDarkDark);
				btnLaunchTorpedo3.Enabled = false;
				rov.FireTorpedo(3);
			}
		}

		private void btnResetPolyBot_Click(object sender, EventArgs e)
		{
			rov.ResetPolyBot();
		}

		private void btnGetVersions_Click(object sender, EventArgs e)
		{
			rov.RequestVersions();
		}

		private void btnResetTorpedoes_Click(object sender, EventArgs e)
		{
			torpedo1Clicks = 0;
			torpedo2Clicks = 0;
			torpedo3Clicks = 0;
			btnLaunchTorpedo1.BackColor = Color.FromKnownColor(KnownColor.LightGray);
			btnLaunchTorpedo2.BackColor = Color.FromKnownColor(KnownColor.LightGray);
			btnLaunchTorpedo3.BackColor = Color.FromKnownColor(KnownColor.LightGray);
			btnLaunchTorpedo1.Enabled = true;
			btnLaunchTorpedo2.Enabled = true;
			btnLaunchTorpedo3.Enabled = true;
			rov.ResetTorpedoStates();
		}

		private void sliderLights_ValueChanged(object sender, EventArgs e)
		{
			rov.LightBrightness = (byte)sliderLights.Value;
			txtLightBrightness.Text = sliderLights.Value.ToString();
		}

		private void txtLightBrightness_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				string input = txtLightBrightness.Text.Trim();
				try
				{
					this.sliderLights.Value = byte.Parse(input);
				}
				catch (Exception ex)
				{
					//revert value
					txtLightBrightness.Text = this.sliderLights.Value.ToString();
				}
				txtLightBrightness.SelectAll();
			}
		}

		private void sliderX_ValueChanged(object sender, EventArgs e)
		{
			rov.LeftThruster = (byte)sliderX.Value;
		}

		private void sliderY_ValueChanged(object sender, EventArgs e)
		{
			rov.RightThruster = (byte)sliderY.Value;
		}

		private void sliderVertical_ValueChanged(object sender, EventArgs e)
		{
			rov.VerticalThruster = (byte)sliderVertical.Value;
		}

		private void sliderSideways_ValueChanged(object sender, EventArgs e)
		{
			rov.SideThruster = (byte)sliderSideways.Value;
		}

		private void sliderWrist_ValueChanged(object sender, EventArgs e)
		{
			rov.ArmWrist = (byte)sliderWrist.Value;
		}

		private void sliderGripper_ValueChanged(object sender, EventArgs e)
		{
			rov.Gripper = (byte)sliderGripper.Value;
		}
	}
}
